<template>
  <div>
    <BasicTable @register="registerTable">
      <template #bodyCell="{ column, record }">
        <template v-if="column.key === 'action'">
          <TableAction
            :actions="[
              {
                label: '修改',
                tooltip: '修改',
                onClick: handleAdd.bind(null, 2, record),
              },
              {
                label: '新增',
                tooltip: '新增',
                onClick: handleAdd.bind(null, 1, { parentId: record.id }),
              },
              {
                label: '删除',
                tooltip: '删除',
                onClick: handleDel.bind(null, record),
              },
            ]"
        /></template>
      </template>
      <template #enabled="{ column, record }">
        <Badge
          v-if="column.dataIndex === 'enabled'"
          :status="statusMap[record.enabled].status"
          :text="statusMap[record.enabled].text"
        />
      </template>
      <template #isInternalInterface="{ column, record }">
        <span v-if="column.dataIndex === 'isInternalInterface'"
          >{{ isInternalInterfaceMap[record.isInternalInterface] }}
        </span>
      </template>
    </BasicTable>
    <AddedModal @register="register" @reload="reload" />
  </div>
</template>

<script lang="ts" setup>
  import { BasicTable, useTable, TableAction } from '@/components/Table';
  import AddedModal from './modal/ added.vue';
  import { Badge, Modal } from 'ant-design-vue';
  import { FieldLibraryDel, FieldTypeList, List } from '@/api/platform/field';
  import { getBasicColumns } from './tableData';
  import { ref } from 'vue';
  import { useModal } from '@/components/Modal';
  import { IFieldListResult } from '@/api/platform/field/type';
  import { useMessage } from '@/hooks/web/useMessage';

  const { createMessage } = useMessage();

  const statusMap = ref({
    1: {
      status: 'success',
      text: '正常',
    },
    0: {
      status: 'default',
      text: '禁用',
    },
  });

  const isInternalInterfaceMap = ref({
    0: '-',
    1: '系统分类',
    2: '字典',
    3: '天眼查',
  });

  const [registerTable, { reload }] = useTable({
    title: '',
    api: List,
    columns: getBasicColumns(),
    rowKey: 'id',
    formConfig: {
      labelWidth: 100,
      schemas: [
        {
          field: `name`,
          label: `字段名称`,
          component: 'Input',
          colProps: {
            span: 5,
          },
        },
        {
          field: `dataType`,
          label: `对应字段`,
          component: 'Input',
          colProps: {
            span: 5,
          },
        },
        {
          field: `itemTypeId`,
          label: `字段类型`,
          component: 'ApiSelect',
          colProps: {
            span: 5,
          },
          componentProps: {
            api: FieldTypeList,
            labelField: 'name',
            valueField: 'id',
          },
        },
        {
          field: `isInternalInterface`,
          label: `字段来源`,
          component: 'Select',
          colProps: {
            span: 5,
          },
          componentProps: {
            options: [
              { label: '系统分类', value: 1 },
              { label: '字典', value: 2 },
            ],
          },
        },
      ],
    },
    useSearchForm: true,
    showTableSetting: true,
    tableSetting: { fullScreen: true },
    showIndexColumn: false,
    clickToRowSelect: false,
    actionColumn: {
      width: 120,
      title: '操作',
      fixed: 'right',
      dataIndex: 'action',
    },
    scroll: { x: 1300 },
  });

  const [register, { openModal }] = useModal();

  function handleAdd(type: number, row: any) {
    openModal(true, {
      type,
      row,
    });
  }

  function handleDel(row: IFieldListResult) {
    Modal.confirm({
      title: '提示',
      content: '该操作将删除该字段',
      onOk: async () => {
        await FieldLibraryDel(row.id);
        createMessage.success('删除成功！');
        reload();
      },
    });
  }
</script>
